<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="部门id" prop="deptId">
      <el-input v-model="dataForm.deptId" placeholder="部门id"></el-input>
    </el-form-item>
    <el-form-item label="基地名称" prop="baseName">
      <el-input v-model="dataForm.baseName" placeholder="基地名称"></el-input>
    </el-form-item>
    <el-form-item label="基地位置" prop="baseLocation">
      <el-input v-model="dataForm.baseLocation" placeholder="基地位置"></el-input>
    </el-form-item>
    <el-form-item label="基地状态" prop="status">
      <!-- <el-input v-model="dataForm.status" placeholder="基地状态"></el-input> -->
            <el-radio-group v-model="dataForm.status">
              <el-radio-button label="1">已启用</el-radio-button>
              <el-radio-button label="2">未启用</el-radio-button>
            </el-radio-group>
    </el-form-item>
    <el-form-item label="基地面积" prop="baseArea">
      <el-input v-model="dataForm.baseArea" placeholder="基地面积"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/pbms/arcbase'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          baseId: 0,
          deptId: '',
          baseName: '',
          baseLocation: '',
          status: 1,
          baseArea: ''
        },
        dataRule: {
          deptId: [
            { required: true, message: '部门id不能为空', trigger: 'blur' }
          ],
          baseName: [
            { required: true, message: '基地名称不能为空', trigger: 'blur' },
            { min: 2, max: 60, message: "长度在 2 到 60 个字符", trigger: "blur" }
          ],
          baseLocation: [
            { required: true, message: '基地位置不能为空', trigger: 'blur' },
            { min: 2, max: 30, message: "长度在 2 到 30 个字符", trigger: "blur" }
          ],
          status: [
            { required: true, message: '基地状态不能为空', trigger: 'blur' }
          ],
          baseArea: [
            { required: true, message: '基地面积不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.baseId = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.baseId) {
            getObj(this.dataForm.baseId).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.baseId) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
